<!DOCTYPE html>
<html>
  <head><meta name="generator" content="Hexo 3.8.0">
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport">
  <meta name="description" content="fengzhaoyang&#39;s blog">
  <meta name="keyword" content="hexo-theme, vuejs">
  
  <link rel="shortcut icon" href="/css/images/logo.png">
  
  <title>
    
    mysql 用户权限配置 | fzy-blog
    
  </title>
  <link href="//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
  <link href="//cdnjs.cloudflare.com/ajax/libs/nprogress/0.2.0/nprogress.min.css" rel="stylesheet">
  <link href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/tomorrow.min.css" rel="stylesheet">
  <link rel="stylesheet" href="/css/style.css">
  
  <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script src="//cdnjs.cloudflare.com/ajax/libs/geopattern/1.2.3/js/geopattern.min.js"></script>
  <script src="//cdnjs.cloudflare.com/ajax/libs/nprogress/0.2.0/nprogress.min.js"></script>
  
  <script src="/js/qrious.js"></script>
  
  
  
  
    <!-- MathJax support START -->
    <script type="text/x-mathjax-config">
      MathJax.Hub.Config({
        tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
        }
      });
    </script>

    <script type="text/x-mathjax-config">
      MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for (i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
        }
      });
    </script>
    <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
    <!-- MathJax support END -->
  


</head>
<div class="wechat-share">
  <img src="/css/images/logo.png">
</div>
  <body>
    <header class="header fixed-header">
  <div class="header-container">
    <a class="home-link" href="/">
      <div class="logo"></div>
      <span>fzy-blog</span>
    </a>
    <ul class="right-list">
      
        <li class="list-item">
          
            <a href="/" class="item-link">Home</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/tags/" class="item-link">Tags</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/archives/" class="item-link">Archives</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/project/" class="item-link">Projects</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/about/" class="item-link">About</a>
          
        </li>
      
    </ul>
    <div class="menu">
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
    </div>
    <div class="menu-mask">
      <ul class="menu-list">
        
          <li class="menu-item">
            
              <a href="/" class="menu-link">Home</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/tags/" class="menu-link">Tags</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/archives/" class="menu-link">Archives</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/project/" class="menu-link">Projects</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/about/" class="menu-link">About</a>
            
          </li>
        
      </ul>
    </div>
  </div>
</header>

    <div id="article-banner">
  <h2>mysql 用户权限配置</h2>
  <p class="post-date">2019-05-24</p>
  <div class="arrow-down">
    <a href="javascript:;"></a>
  </div>
</div>
<main class="app-body flex-box">
  <!-- Article START -->
  <article class="post-article">
    <section class="markdown-content"><h1 id="mysql-用户权限配置"><a href="#mysql-用户权限配置" class="headerlink" title="mysql 用户权限配置"></a>mysql 用户权限配置</h1><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br></pre></td><td class="code"><pre><span class="line">1.远程登录mysql</span><br><span class="line">mysql -h ip -u root -p 密码</span><br><span class="line">2.创建用户</span><br><span class="line">格式：<span class="keyword">grant</span> 权限 <span class="keyword">on</span> 数据库.* <span class="keyword">to</span> 用户名@登录主机 <span class="keyword">identified</span> <span class="keyword">by</span> “密码”;</span><br><span class="line">例1：增加一个test1用户，密码为123456，可以在任何主机上登录，并对所有数据库有查询，增加，修改和删除的功能。需要在mysql的root用户下进行</span><br><span class="line">mysql&gt;grant select,insert,update,delete on *.* to test1@”%” identified by “123456″；</span><br><span class="line">mysql&gt;flush privileges;</span><br><span class="line">例2：增加一个test2用户，密码为123456，只能在192.168.2.12上登录，并对数据库student有查询，增加，修改和删除的功能。需要在mysql的root用户下进行</span><br><span class="line">mysql&gt;grant select,insert,update,delete on student.* to test2@192.168.2.12 identified by “123456″;</span><br><span class="line">mysql&gt;flush privileges;</span><br><span class="line">例3：授权用户test3拥有数据库student的所有权限</span><br><span class="line">mysql&gt;grant all privileges on student.* to test3@localhost identified by ’123456′;</span><br><span class="line">mysql&gt;flush privileges;</span><br><span class="line">3.修改用户密码</span><br><span class="line">mysql&gt;update mysql.user set password=password(’123456′) where User=’test1′ and Host=’localhost’;</span><br><span class="line">mysql&gt;flush privileges;</span><br><span class="line">4.删除用户</span><br><span class="line">mysql&gt;delete from user where user=’test2′ and host=’localhost’;</span><br><span class="line">mysql&gt;flush privileges;</span><br><span class="line">5.删除数据库和删除表</span><br><span class="line">mysql&gt;drop database 数据库名;</span><br><span class="line">mysql&gt;drop table 表名;</span><br><span class="line">6.删除账户及权限</span><br><span class="line"><span class="keyword">drop</span> <span class="keyword">user</span> 用户名@’%’</span><br><span class="line"><span class="keyword">drop</span> <span class="keyword">user</span> 用户名@localhost</span><br><span class="line">**************************************************************************************</span><br><span class="line"><span class="keyword">grant</span> 详细解析如下：</span><br><span class="line">**************************************************************************************</span><br><span class="line">MySQL 赋予用户权限命令的简单格式可概括为：</span><br><span class="line"><span class="keyword">grant</span> 权限 <span class="keyword">on</span> 数据库对象 <span class="keyword">to</span> 用户</span><br><span class="line">一、<span class="keyword">grant</span> 普通数据用户，查询、插入、更新、删除 数据库中所有表数据的权利。</span><br><span class="line"><span class="keyword">grant</span> <span class="keyword">select</span> <span class="keyword">on</span> testdb.* <span class="keyword">to</span> common_user@’%’</span><br><span class="line"><span class="keyword">grant</span> <span class="keyword">insert</span> <span class="keyword">on</span> testdb.* <span class="keyword">to</span> common_user@’%’</span><br><span class="line"><span class="keyword">grant</span> <span class="keyword">update</span> <span class="keyword">on</span> testdb.* <span class="keyword">to</span> common_user@’%’</span><br><span class="line"><span class="keyword">grant</span> <span class="keyword">delete</span> <span class="keyword">on</span> testdb.* <span class="keyword">to</span> common_user@’%’</span><br><span class="line">或者，用一条 MySQL 命令来替代：</span><br><span class="line"><span class="keyword">grant</span> <span class="keyword">select</span>, <span class="keyword">insert</span>, <span class="keyword">update</span>, <span class="keyword">delete</span> <span class="keyword">on</span> testdb.* <span class="keyword">to</span> common_user@’%’</span><br><span class="line">二、<span class="keyword">grant</span> 数据库开发人员，创建表、索引、视图、存储过程、函数。。。等权限。</span><br><span class="line"><span class="keyword">grant</span> 创建、修改、删除 MySQL 数据表结构权限。</span><br><span class="line"><span class="keyword">grant</span> <span class="keyword">create</span> <span class="keyword">on</span> testdb.* <span class="keyword">to</span> developer@’<span class="number">192.168</span><span class="number">.0</span>.%’;</span><br><span class="line"><span class="keyword">grant</span> <span class="keyword">alter</span> <span class="keyword">on</span> testdb.* <span class="keyword">to</span> developer@’<span class="number">192.168</span><span class="number">.0</span>.%’;</span><br><span class="line"><span class="keyword">grant</span> <span class="keyword">drop</span> <span class="keyword">on</span> testdb.* <span class="keyword">to</span> developer@’<span class="number">192.168</span><span class="number">.0</span>.%’;</span><br><span class="line"><span class="keyword">grant</span> 操作 MySQL 外键权限。</span><br><span class="line"><span class="keyword">grant</span> <span class="keyword">references</span> <span class="keyword">on</span> testdb.* <span class="keyword">to</span> developer@’<span class="number">192.168</span><span class="number">.0</span>.%’;</span><br><span class="line"><span class="keyword">grant</span> 操作 MySQL 临时表权限。</span><br><span class="line"><span class="keyword">grant</span> <span class="keyword">create</span> <span class="keyword">temporary</span> <span class="keyword">tables</span> <span class="keyword">on</span> testdb.* <span class="keyword">to</span> developer@’<span class="number">192.168</span><span class="number">.0</span>.%’;</span><br><span class="line"><span class="keyword">grant</span> 操作 MySQL 索引权限。</span><br><span class="line"><span class="keyword">grant</span> <span class="keyword">index</span> <span class="keyword">on</span> testdb.* <span class="keyword">to</span> developer@’<span class="number">192.168</span><span class="number">.0</span>.%’;</span><br><span class="line"><span class="keyword">grant</span> 操作 MySQL 视图、查看视图源代码 权限。</span><br><span class="line"><span class="keyword">grant</span> <span class="keyword">create</span> <span class="keyword">view</span> <span class="keyword">on</span> testdb.* <span class="keyword">to</span> developer@’<span class="number">192.168</span><span class="number">.0</span>.%’;</span><br><span class="line"><span class="keyword">grant</span> <span class="keyword">show</span> <span class="keyword">view</span> <span class="keyword">on</span> testdb.* <span class="keyword">to</span> developer@’<span class="number">192.168</span><span class="number">.0</span>.%’;</span><br><span class="line"><span class="keyword">grant</span> 操作 MySQL 存储过程、函数 权限。</span><br><span class="line"><span class="keyword">grant</span> <span class="keyword">create</span> routine <span class="keyword">on</span> testdb.* <span class="keyword">to</span> developer@’<span class="number">192.168</span><span class="number">.0</span>.%’; — now, can <span class="keyword">show</span> <span class="keyword">procedure</span> <span class="keyword">status</span></span><br><span class="line"><span class="keyword">grant</span> <span class="keyword">alter</span> routine <span class="keyword">on</span> testdb.* <span class="keyword">to</span> developer@’<span class="number">192.168</span><span class="number">.0</span>.%’; — now, you can <span class="keyword">drop</span> a <span class="keyword">procedure</span></span><br><span class="line"><span class="keyword">grant</span> <span class="keyword">execute</span> <span class="keyword">on</span> testdb.* <span class="keyword">to</span> developer@’<span class="number">192.168</span><span class="number">.0</span>.%’;</span><br><span class="line">三、<span class="keyword">grant</span> 普通 DBA 管理某个 MySQL 数据库的权限。</span><br><span class="line"><span class="keyword">grant</span> <span class="keyword">all</span> <span class="keyword">privileges</span> <span class="keyword">on</span> testdb <span class="keyword">to</span> dba@’localhost’</span><br><span class="line">其中，关键字 “<span class="keyword">privileges</span>” 可以省略。</span><br><span class="line">四、<span class="keyword">grant</span> 高级 DBA 管理 MySQL 中所有数据库的权限。</span><br><span class="line"><span class="keyword">grant</span> <span class="keyword">all</span> <span class="keyword">on</span> *.* <span class="keyword">to</span> dba@’localhost’</span><br><span class="line">五、MySQL <span class="keyword">grant</span> 权限，分别可以作用在多个层次上。</span><br><span class="line"><span class="number">1.</span> <span class="keyword">grant</span> 作用在整个 MySQL 服务器上：</span><br><span class="line"><span class="keyword">grant</span> <span class="keyword">select</span> <span class="keyword">on</span> *.* <span class="keyword">to</span> dba@localhost; — dba 可以查询 MySQL 中所有数据库中的表。</span><br><span class="line"><span class="keyword">grant</span> <span class="keyword">all</span> <span class="keyword">on</span> *.* <span class="keyword">to</span> dba@localhost; — dba 可以管理 MySQL 中的所有数据库</span><br><span class="line">2. <span class="keyword">grant</span> 作用在单个数据库上：</span><br><span class="line"><span class="keyword">grant</span> <span class="keyword">select</span> <span class="keyword">on</span> testdb.* <span class="keyword">to</span> dba@localhost; — dba 可以查询 testdb 中的表。</span><br><span class="line">3. <span class="keyword">grant</span> 作用在单个数据表上：</span><br><span class="line"><span class="keyword">grant</span> <span class="keyword">select</span>, <span class="keyword">insert</span>, <span class="keyword">update</span>, <span class="keyword">delete</span> <span class="keyword">on</span> testdb.orders <span class="keyword">to</span> dba@localhost;</span><br><span class="line">4. <span class="keyword">grant</span> 作用在表中的列上：</span><br><span class="line"><span class="keyword">grant</span> <span class="keyword">select</span>(<span class="keyword">id</span>, se, <span class="keyword">rank</span>) <span class="keyword">on</span> testdb.apache_log <span class="keyword">to</span> dba@localhost;</span><br><span class="line">5. <span class="keyword">grant</span> 作用在存储过程、函数上：</span><br><span class="line"><span class="keyword">grant</span> <span class="keyword">execute</span> <span class="keyword">on</span> <span class="keyword">procedure</span> testdb.pr_add <span class="keyword">to</span> ‘dba’@’localhost’</span><br><span class="line"><span class="keyword">grant</span> <span class="keyword">execute</span> <span class="keyword">on</span> <span class="keyword">function</span> testdb.fn_add <span class="keyword">to</span> ‘dba’@’localhost’</span><br><span class="line">六、查看 MySQL 用户权限</span><br><span class="line">查看当前用户（自己）权限：</span><br><span class="line"><span class="keyword">show</span> <span class="keyword">grants</span>;</span><br><span class="line">查看其他 MySQL 用户权限：</span><br><span class="line"><span class="keyword">show</span> <span class="keyword">grants</span> <span class="keyword">for</span> dba@localhost;</span><br><span class="line">七、撤销已经赋予给 MySQL 用户权限的权限。</span><br><span class="line"><span class="keyword">revoke</span> 跟 <span class="keyword">grant</span> 的语法差不多，只需要把关键字 “<span class="keyword">to</span>” 换成 “<span class="keyword">from</span>” 即可：</span><br><span class="line"><span class="keyword">grant</span> <span class="keyword">all</span> <span class="keyword">on</span> *.* <span class="keyword">to</span> dba@localhost;</span><br><span class="line"><span class="keyword">revoke</span> <span class="keyword">all</span> <span class="keyword">on</span> *.* <span class="keyword">from</span> dba@localhost;</span><br><span class="line">八、MySQL <span class="keyword">grant</span>、<span class="keyword">revoke</span> 用户权限注意事项</span><br><span class="line"><span class="number">1.</span> <span class="keyword">grant</span>, <span class="keyword">revoke</span> 用户权限后，该用户只有重新连接 MySQL 数据库，权限才能生效。</span><br><span class="line"><span class="number">2.</span> 如果想让授权的用户，也可以将这些权限 <span class="keyword">grant</span> 给其他用户，需要选项 “<span class="keyword">grant</span> <span class="keyword">option</span>“</span><br><span class="line"><span class="keyword">grant</span> <span class="keyword">select</span> <span class="keyword">on</span> testdb.* <span class="keyword">to</span> dba@localhost <span class="keyword">with</span> <span class="keyword">grant</span> <span class="keyword">option</span>;</span><br><span class="line">这个特性一般用不到。实际中，数据库权限最好由 DBA 来统一管理。</span><br><span class="line">Category: Post</span><br><span class="line">You can follow any responses to this entry via RSS.</span><br><span class="line">Comments are currently closed, but you can trackback from your own site.</span><br><span class="line">=========================================================================</span><br><span class="line">1.创建用户并授权</span><br><span class="line"><span class="keyword">grant</span>语句的语法:</span><br><span class="line"><span class="keyword">grant</span> <span class="keyword">privileges</span> (<span class="keyword">columns</span>) <span class="keyword">on</span> what <span class="keyword">to</span> <span class="keyword">user</span> <span class="keyword">identified</span> <span class="keyword">by</span> “<span class="keyword">password</span>” <span class="keyword">with</span> <span class="keyword">grant</span> <span class="keyword">option</span></span><br><span class="line">要使用该句型，需确定字段有：</span><br><span class="line"><span class="keyword">privileges</span> 权限指定符权限允许的操作</span><br><span class="line"><span class="keyword">alter</span> 修改表和索引</span><br><span class="line"><span class="keyword">create</span> 创建数据库和表</span><br><span class="line"><span class="keyword">delete</span> 删除表中已有的记录</span><br><span class="line"><span class="keyword">drop</span> 抛弃（删除）数据库和表</span><br><span class="line"><span class="keyword">index</span> 创建或抛弃索引</span><br><span class="line"><span class="keyword">insert</span> 向表中插入新行</span><br><span class="line"><span class="keyword">reference</span> 未用</span><br><span class="line"><span class="keyword">select</span> 检索表中的记录</span><br><span class="line"><span class="keyword">update</span> 修改现存表记录</span><br><span class="line"><span class="keyword">file</span> 读或写服务器上的文件</span><br><span class="line">process 查看服务器中执行的线程信息或杀死线程</span><br><span class="line">reload 重载授权表或清空日志、主机缓存或表缓存。</span><br><span class="line"><span class="keyword">shutdown</span> 关闭服务器</span><br><span class="line"><span class="keyword">all</span> 所有；</span><br><span class="line"><span class="keyword">all</span> <span class="keyword">privileges</span>同义词</span><br><span class="line"><span class="keyword">usage</span> 特殊的“无权限”权限</span><br><span class="line">以上权限分三组:</span><br><span class="line">第一组:适用于数据库、表和列如:<span class="keyword">alter</span> <span class="keyword">create</span> <span class="keyword">delete</span> <span class="keyword">drop</span> <span class="keyword">index</span> <span class="keyword">insert</span> <span class="keyword">select</span> <span class="keyword">update</span></span><br><span class="line">第二组:数管理权限 它们允许用户影响服务器的操作 需严格地授权 如:<span class="keyword">file</span> process reload shut*</span><br><span class="line">第三组:权限特殊 <span class="keyword">all</span>意味着“所有权限” uasge意味着无权限，即创建用户，但不授予权限</span><br><span class="line"><span class="keyword">columns</span></span><br><span class="line">权限运用的列(可选)并且你只能设置列特定的权限。如果命令有多于一个列，应该用逗号分开它们。</span><br><span class="line">what</span><br><span class="line">权限运用的级别。权限可以是全局,定数据库或特定表.</span><br><span class="line"><span class="keyword">user</span></span><br><span class="line">权限授予的用户，由一个用户名和主机名组成,许两个同名用户从不同地方连接.缺省:mysql用户<span class="keyword">password</span></span><br><span class="line">赋予用户的口令(可选),如果你对用户没有指定<span class="keyword">identified</span> <span class="keyword">by</span>子句,该用户口令不变.</span><br><span class="line">用<span class="keyword">identified</span> <span class="keyword">by</span>时，口令字符串用改用口令的字面含义,<span class="keyword">grant</span>将为你编码口令.</span><br><span class="line">注:<span class="keyword">set</span> <span class="keyword">password</span>使用<span class="keyword">password</span>()函数</span><br><span class="line"><span class="keyword">with</span> <span class="keyword">grant</span> <span class="keyword">option</span></span><br><span class="line">用户可以授予权限通过<span class="keyword">grant</span>语句授权给其它用户(可选)</span><br><span class="line">实例讲解:</span><br><span class="line"><span class="keyword">grant</span> <span class="keyword">all</span> <span class="keyword">on</span> db_book.* <span class="keyword">to</span> huaying@koowo.com <span class="keyword">identified</span> <span class="keyword">by</span> “yeelion”   只能在本地连接</span><br><span class="line"><span class="keyword">grant</span> <span class="keyword">all</span> <span class="keyword">on</span> db_book.* <span class="keyword">to</span> huaying@vpn.koowo.com <span class="keyword">identified</span> <span class="keyword">by</span> “yeeliong”  允许从此域连接</span><br><span class="line"><span class="keyword">grant</span> <span class="keyword">all</span> <span class="keyword">on</span> db_book.* <span class="keyword">to</span> huaying@% <span class="keyword">identified</span> <span class="keyword">by</span> “yeelion”   允许从任何主机连接 注:”%”字符起通配符作用，与<span class="keyword">like</span>模式匹配的含义相同。</span><br><span class="line"><span class="keyword">grant</span> <span class="keyword">all</span> <span class="keyword">on</span> db_book.* <span class="keyword">to</span> huaying@%.koowo.com <span class="keyword">identified</span> <span class="keyword">by</span> “yeelion”;  允许huaying从koowo.com域的任何主机连接</span><br><span class="line"><span class="keyword">grant</span> <span class="keyword">all</span> <span class="keyword">on</span> db_book.* <span class="keyword">to</span> huaying@<span class="number">192.168</span><span class="number">.1</span><span class="number">.189</span> <span class="keyword">identified</span> <span class="keyword">by</span> “yeelion”</span><br><span class="line"><span class="keyword">grant</span> <span class="keyword">all</span> <span class="keyword">on</span> db_book.* <span class="keyword">to</span> huaying@<span class="number">192.168</span><span class="number">.1</span>.% <span class="keyword">identified</span> <span class="keyword">by</span> “yeelion”</span><br><span class="line"><span class="keyword">grant</span> <span class="keyword">all</span> <span class="keyword">on</span> db_book.* <span class="keyword">to</span> huaying@<span class="number">192.168</span><span class="number">.1</span><span class="number">.0</span>/<span class="number">17</span> <span class="keyword">identified</span> <span class="keyword">by</span> “yeelion”</span><br><span class="line">允许从单IP 段IP或一子网IP登陆</span><br><span class="line">注:有时 用户@IP 需用引号 如”huaying@<span class="number">192.168</span><span class="number">.1</span><span class="number">.0</span>/<span class="number">17</span>″</span><br><span class="line"><span class="keyword">grant</span> <span class="keyword">all</span> <span class="keyword">on</span> *.* <span class="keyword">to</span> huaying@localhost <span class="keyword">identified</span> <span class="keyword">by</span> “yeelion” <span class="keyword">with</span> <span class="keyword">grant</span> <span class="keyword">option</span></span><br><span class="line">添加超级用户huaying　可在本地登陆做任何操作.</span><br><span class="line"><span class="keyword">grant</span> reload <span class="keyword">on</span> *.* <span class="keyword">to</span> huaying@localhost <span class="keyword">identified</span> <span class="keyword">by</span> “yeelion” 只赋予reload权限</span><br><span class="line"><span class="keyword">grant</span> <span class="keyword">all</span> <span class="keyword">on</span> db_book <span class="keyword">to</span> huaying@koowo.com indetified <span class="keyword">by</span> “yeelion” 所有权限</span><br><span class="line"><span class="keyword">grant</span> <span class="keyword">select</span> <span class="keyword">on</span> db_book <span class="keyword">to</span> huaying@% indetified <span class="keyword">by</span> “yeelion” 只读权限</span><br><span class="line"><span class="keyword">grant</span> <span class="keyword">select</span>,<span class="keyword">insert</span>,<span class="keyword">delete</span>,<span class="keyword">update</span> <span class="keyword">on</span> db_book <span class="keyword">to</span> huaying@koowo.com indetified <span class="keyword">by</span> “yeelion”</span><br><span class="line">只有<span class="keyword">select</span>,<span class="keyword">insert</span>,<span class="keyword">delete</span>,<span class="keyword">update</span>的权限</span><br><span class="line"><span class="keyword">grant</span> <span class="keyword">select</span> <span class="keyword">on</span> db_book.storybook <span class="keyword">to</span> huaying@localhost indetified <span class="keyword">by</span> “yeelion”　只对表</span><br><span class="line"><span class="keyword">grant</span> <span class="keyword">update</span> (<span class="keyword">name</span>) <span class="keyword">on</span> db_book.storybook <span class="keyword">to</span> huaying@localhost 只对表的<span class="keyword">name</span>列 密码不变</span><br><span class="line"><span class="keyword">grant</span> <span class="keyword">update</span> (<span class="keyword">id</span>,<span class="keyword">name</span>,author) <span class="keyword">on</span> db_book.storybook <span class="keyword">to</span> huaying@localhost 只对表的多列</span><br><span class="line"><span class="keyword">grant</span> <span class="keyword">all</span> <span class="keyword">on</span> book.* <span class="keyword">to</span> “”@koowo.com 允许koowo.com域中的所有用户使用库book</span><br><span class="line"><span class="keyword">grant</span> <span class="keyword">all</span> <span class="keyword">on</span> book.* <span class="keyword">to</span> huaying@%.koowo.com indetified <span class="keyword">by</span> “yeelion” <span class="keyword">with</span> <span class="keyword">grant</span> <span class="keyword">option</span></span><br><span class="line">允许huaying对库book所有表的管理员授权.</span><br><span class="line"><span class="number">2.</span>撤权并删除用户</span><br><span class="line"><span class="keyword">revoke</span>的语法类似于<span class="keyword">grant</span>语句</span><br><span class="line"><span class="keyword">to</span>用<span class="keyword">from</span>取代,没有indetifed <span class="keyword">by</span>和<span class="keyword">with</span> <span class="keyword">grant</span> <span class="keyword">option</span>子句. 如下：</span><br><span class="line"><span class="keyword">revoke</span> <span class="keyword">privileges</span> (<span class="keyword">columns</span>) <span class="keyword">on</span> what <span class="keyword">from</span> <span class="keyword">user</span></span><br><span class="line"><span class="keyword">user</span>:必须匹配原来<span class="keyword">grant</span>语句的你想撤权的用户的<span class="keyword">user</span>部分。</span><br><span class="line"><span class="keyword">privileges</span>:不需匹配,可以用<span class="keyword">grant</span>语句授权,然后用<span class="keyword">revoke</span>语句只撤销部分权限。</span><br><span class="line"><span class="keyword">revoke</span>语句只删权限不删用户,撤销了所有权限后<span class="keyword">user</span>表中用户记录保留,用户仍然可以连接服务器.</span><br><span class="line">要完全删除一个用户必须用一条<span class="keyword">delete</span>语句明确从<span class="keyword">user</span>表中删除用户记录：</span><br><span class="line"><span class="keyword">delete</span> <span class="keyword">from</span> <span class="keyword">user</span> <span class="keyword">where</span> <span class="keyword">user</span>=”huaying”</span><br><span class="line"><span class="keyword">flush</span> <span class="keyword">privileges</span>;　重载授权表</span><br><span class="line">注：使用<span class="keyword">grant</span>和<span class="keyword">revoke</span>语句时，表自动重载，而你直接修改授权表时不是.</span><br><span class="line">实例：</span><br><span class="line"><span class="number">1.</span>创建数据库</span><br><span class="line"><span class="keyword">CREATE</span> <span class="keyword">DATABASE</span>  <span class="string">`fypay`</span> <span class="keyword">DEFAULT</span> <span class="built_in">CHARACTER</span> <span class="keyword">SET</span> utf8 <span class="keyword">COLLATE</span> utf8_general_ci;</span><br><span class="line">2.为创建的数据库增加用户fypay</span><br><span class="line"><span class="keyword">grant</span> <span class="keyword">create</span>,<span class="keyword">select</span>,<span class="keyword">insert</span>,<span class="keyword">update</span>,<span class="keyword">delete</span>,<span class="keyword">drop</span>,<span class="keyword">alter</span> <span class="keyword">on</span> fypay.* <span class="keyword">to</span> fypay@”%” <span class="keyword">identified</span> <span class="keyword">by</span> “testfpay”;</span><br><span class="line">3.删除fypay用户</span><br><span class="line"><span class="keyword">delete</span> <span class="keyword">from</span> <span class="keyword">user</span> <span class="keyword">where</span> <span class="keyword">user</span>=”fypay”</span><br><span class="line"><span class="keyword">drop</span> <span class="keyword">user</span> fypay@localhost</span><br><span class="line"><span class="number">4.</span>刷新数据库</span><br><span class="line"><span class="keyword">flush</span> <span class="keyword">privileges</span>;</span><br></pre></td></tr></table></figure>
</section>
    <!-- Tags START -->
    
    <div class="tags">
      <span>Tags:</span>
      
  <a href="/tags#DB">
    <span class="tag-code">DB</span>
  </a>

    </div>
    
    <!-- Tags END -->
    <!-- NAV START -->
    
  <div class="nav-container">
    <!-- reverse left and right to put prev and next in a more logic postition -->
    
      <a class="nav-left" href="/2019/05/24/Java8/BigDecimal保留两位小数/">
        <span class="nav-arrow">← </span>
        
          BigDecimal 保留两位小数
        
      </a>
    
    
      <a class="nav-right" href="/2019/05/24/DB/数据库查询优化方案处理上百万级记录如何提高处理查询速度/">
        
          数据库查询优化方案处理上百万级记录如何提高处理查询速度.md
        
        <span class="nav-arrow"> →</span>
      </a>
    
  </div>

    <!-- NAV END -->
    <!-- 打赏 START -->
    
    <div class="money-like">
      <div class="reward-btn">
        赏
        <span class="money-code">
          <span class="alipay-code">
            <div class="code-image"></div>
            <b>使用支付宝打赏</b>
          </span>
          <span class="wechat-code">
            <div class="code-image"></div>
            <b>使用微信打赏</b>
          </span>
        </span>
      </div>
      <p class="notice">若你觉得我的文章对你有帮助，欢迎点击上方按钮对我打赏</p>
    </div>
    
    <!-- 打赏 END -->
    <!-- 二维码 START -->
    
    <div class="qrcode">
      <canvas id="share-qrcode"></canvas>
      <p class="notice">扫描二维码，分享此文章</p>
    </div>
    
    <!-- 二维码 END -->
    
    <!-- No Comment -->
    
  </article>
  <!-- Article END -->
  <!-- Catalog START -->
  
  <aside class="catalog-container">
  <div class="toc-main">
    <strong class="toc-title">Catalog</strong>
    
      <ol class="toc-nav"><li class="toc-nav-item toc-nav-level-1"><a class="toc-nav-link" href="#mysql-用户权限配置"><span class="toc-nav-text">mysql 用户权限配置</span></a></li></ol>
    
  </div>
</aside>
  
  <!-- Catalog END -->
</main>

<script>
  (function () {
    var url = 'https://fengzhaoy.github.io/2019/05/24/DB/mysql用户权限配置/';
    var banner = ''
    if (banner !== '' && banner !== 'undefined' && banner !== 'null') {
      $('#article-banner').css({
        'background-image': 'url(' + banner + ')'
      })
    } else {
      $('#article-banner').geopattern(url)
    }
    $('.header').removeClass('fixed-header')

    // error image
    $(".markdown-content img").on('error', function () {
      $(this).attr('src', 'http://file.muyutech.com/error-img.png')
      $(this).css({
        'cursor': 'default'
      })
    })

    // zoom image
    $(".markdown-content img").on('click', function () {
      var src = $(this).attr('src')
      if (src !== 'http://file.muyutech.com/error-img.png') {
        var imageW = $(this).width()
        var imageH = $(this).height()

        var zoom = ($(window).width() * 0.95 / imageW).toFixed(2)
        zoom = zoom < 1 ? 1 : zoom
        zoom = zoom > 2 ? 2 : zoom
        var transY = (($(window).height() - imageH) / 2).toFixed(2)

        $('body').append('<div class="image-view-wrap"><div class="image-view-inner"><img src="' + src +
          '" /></div></div>')
        $('.image-view-wrap').addClass('wrap-active')
        $('.image-view-wrap img').css({
          'width': `${imageW}`,
          'transform': `translate3d(0, ${transY}px, 0) scale3d(${zoom}, ${zoom}, 1)`
        })
        $('html').css('overflow', 'hidden')

        $('.image-view-wrap').on('click', function () {
          $(this).remove()
          $('html').attr('style', '')
        })
      }
    })
  })();
</script>


<script>
  var qr = new QRious({
    element: document.getElementById('share-qrcode'),
    value: document.location.href
  });
</script>





    <div class="scroll-top">
  <span class="arrow-icon"></span>
</div>
    <footer class="app-footer">
  <p class="copyright">
    &copy; 2019
  </p>
</footer>

<script>
  function async (u, c) {
    var d = document,
      t = 'script',
      o = d.createElement(t),
      s = d.getElementsByTagName(t)[0];
    o.src = u;
    if (c) {
      o.addEventListener('load', function (e) {
        c(null, e);
      }, false);
    }
    s.parentNode.insertBefore(o, s);
  }
</script>
<script>
  async ("//cdnjs.cloudflare.com/ajax/libs/fastclick/1.0.6/fastclick.min.js", function () {
    FastClick.attach(document.body);
  })
</script>

<script>
  var hasLine = 'true';
  async ("//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js", function () {
    $('figure pre').each(function (i, block) {
      var figure = $(this).parents('figure');
      if (hasLine === 'false') {
        figure.find('.gutter').hide();
      }
      var lang = figure.attr('class').split(' ')[1] || 'code';
      var codeHtml = $(this).html();
      var codeTag = document.createElement('code');
      codeTag.className = lang;
      codeTag.innerHTML = codeHtml;
      $(this).attr('class', '').empty().html(codeTag);
      figure.attr('data-lang', lang.toUpperCase());
      hljs.highlightBlock(block);
    });
  })
</script>
<!-- Baidu Tongji -->

<script src="/js/script.js"></script>
  </body>
</html>